package com.lbloca.locate.ui.fragment;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.gyf.immersionbar.ImmersionBar;
import com.lbloca.locate.R;
import com.lbloca.locate.ui.base.BaseFragment;
import com.lihang.ShadowLayout;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;

public class LocationFragment extends BaseFragment {

    @BindView(R.id.bmapView)
    MapView mMapView;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.view)
    View line;
    private BaiduMap mBaiduMap;
    private MyLocationConfiguration mLocationConfiguration;
    public BDLocation mLocation;

    public static final LocationFragment getInstance() {
        return new LocationFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.frag_location;
    }

    @Override
    protected void initView(View view) {
        View child = mMapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
        mMapView.showScaleControl(false);
        initMapStatus();
        changeMap();
    }

    private void initMapStatus() {
        mBaiduMap = mMapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);
    }

    public void changeMap() {
        if (mLocation != null && mBaiduMap != null) {
            MyLocationData locData = new MyLocationData.Builder().direction(mLocation.getDirection())
                    .latitude(mLocation.getLatitude())
                    .longitude(mLocation.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);

            mLocationConfiguration = new MyLocationConfiguration(MyLocationConfiguration.LocationMode.NORMAL, true, BitmapDescriptorFactory.fromResource(R.drawable.locqi));
            mBaiduMap.setMyLocationConfiguration(mLocationConfiguration);

            LatLng latLng = new LatLng(mLocation.getLatitude(), mLocation.getLongitude());
            MapStatus.Builder builder = new MapStatus.Builder();
            builder.target(latLng).zoom(17);
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
//            mBaiduMap.clear();
//            //构建Marker图标
//            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.locqi);
//            //构建MarkerOption，用于在地图上添加Marker
//            OverlayOptions option = new MarkerOptions().position(latLng).perspective(false).flat(true)
//                    .icon(bitmap);
//            //在地图上添加Marker，并显示
//            mBaiduMap.addOverlay(option);

            tvAddress.setText(mLocation.getAddrStr());
            tvTime.setText(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
    }

    public void onPause() {
        super.onPause();
        if (mMapView != null)
            mMapView.onPause();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mMapView != null)
            mMapView.onResume();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mBaiduMap != null)
            mBaiduMap.setMyLocationEnabled(false);
        mBaiduMap = null;
        if (mMapView != null)
            mMapView.onDestroy();
        mMapView = null;
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void initData() {

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden) {
            ImmersionBar.with(this).statusBarDarkFont(true, 0.1f).titleBar(line).init();
        }
        super.onHiddenChanged(hidden);
    }
}
